package jmu.edu.yjc.controller;

import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Dept;
import jmu.edu.yjc.service.DeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
@RestController
@RequestMapping("/personal/dept")
public class DeptController {
    @Resource
    private DeptService deptService;

    @GetMapping("/list")
    public ResponseResult getDeptList(Integer pageNum, Integer pageSize,
                                      String deptName, String phonenumber) {
        return deptService.getDeptList(pageNum, pageSize, deptName, phonenumber);
    }

    @PostMapping
    public ResponseResult addDept(@RequestBody Dept dept) {
        deptService.save(dept);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult delDept(@PathVariable("id") String id) {
        String[] ids = id.split(",");
        deptService.removeByIds(Arrays.asList(ids));
        return ResponseResult.okResult();
    }

    @PutMapping
    public ResponseResult updateDept(@RequestBody Dept dept) {
        deptService.updateById(dept);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult getDept(@PathVariable("id") Long id) {
        Dept dept = deptService.getById(id);
        return ResponseResult.okResult(dept);
    }

    @PreAuthorize("@ps.hasPermission('personal:Dept:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        deptService.export(response);
    }
}
